package edu.gyc.homework.io;

import java.io.FileInputStream;

import java.io.ObjectInputStream;

import java.util.*;
//考试类
public class Test {
    //试题库
    private List<Question> questionList = new ArrayList<>();
   //卷子有随机提取的3道题
    private List<Question> paperList = new ArrayList<>();

    private int count=0;

   //读取解密后的文件数据至试题库
    public void readPaper() {
        ObjectInputStream ois=null;

        try {
            ois = new ObjectInputStream(new FileInputStream("d:/shitinew.st"));
            questionList=(List<Question>)ois.readObject();
            System.out.println("试题读取完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
//从试题库随机抽取3道题构成卷子
    public void makePaper() {
        Random random = new Random();
        Set<Integer> set = new HashSet<>();
        while (set.size() < 3) {
            set.add(random.nextInt(5));
        }
        for (Integer i : set) {
            paperList.add(questionList.get(i));
        }
        System.out.println("随机生成题目完毕！");
    }
// 用户逐一对试卷解答并评等级
    public void test() {
        Scanner scanner = new Scanner(System.in);
        for (Question q : paperList) {
            System.out.println(q.getShiti());
            System.out.println("请输入答案：");
            int answer = Integer.parseInt(scanner.nextLine());
            if (answer == q.getAnswer()) {
                count++;
            }
        }
        if (count == 3) {
            System.out.println("考试成绩为A");
        } else if (count == 2) {
            System.out.println("考试成绩为B");
        }else if (count == 1) {
            System.out.println("考试成绩为C");
        }else {
            System.out.println("你太菜了！");
        }
    }

    public static void main(String[] args) {
        Test test=new Test();
        PaperUtil.jm("d:/0505.st","d:/shitinew.st","解密");
        test.readPaper();
        test.makePaper();
        test.test();
    }
}
